/**
 * PreliminarySystemsTest.java
 * ************************** 
 * @date May 8, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package JUnitTests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import infrastructureAPI.ConcreteRequester;
import infrastructureAPI.Service;
import infrastructureAPI.APIMessages.DispatcherRequestMessage;
import infrastructureAPI.APIMessages.GeneralRequestMessage;
import infrastructureAPI.APIMessages.RequestMessage;
import infrastructureAPI.algorithms.APIFunction;

import java.net.InetAddress;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.Vector;

import messages.GeneralMessage;
import messages.Message;
import networkRmi.NetworkManager;

import org.junit.Before;
import org.junit.Test;

import systems.dispatcher.DispatcherSystem;
import systems.network.GeneralSystemsMessageDispatcher;

/**
 * Description: 
 *
 */
public class PreliminarySystemsTest {
	
	
	public class MySysNetMan extends GeneralSystemsMessageDispatcher{

		/**
		 * @param netMan
		 * @param incomingMsgClass
		 */
		public MySysNetMan(NetworkManager netMan, Class<?> incomingMsgClass) {
			super(netMan, DispatcherRequestMessage.class);
		}
		
		public Queue<Message> getReceivedQueueMessage(){
			return receivedSystemMessages;
		}
		
		public Queue<Message> getRawQueueMessage(){
			return rawReceivedMessages;
		}
	}
	
	public class MyApi implements APIFunction{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public RequestMessage createRequestMessage(InetAddress targetAddress, 
				String targetNodeName) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean initializeServices(List<Service> services) {
			System.out.println("executingAlgorithm");
			return false;			
		}

		@Override
		public List<Class<?>> requiredServices() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Service extractSuppliedService() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean requireSystemRegistration() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public Service extractServiceToDeRegister() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean requireSystemDeRegistration() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean requireReply() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	FakeNetMan fakeNetMan = new FakeNetMan();
	MySysNetMan sysNetMan ;
	Queue<Message> incomingMessages;
	List<Service> registeredServices;
	
	int numberRequests=0;
	int numberDispReq=0;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		sysNetMan = new MySysNetMan(fakeNetMan, DispatcherRequestMessage.class);
		incomingMessages = new ArrayDeque<Message>();
		
		// fill with Message(s)
		incomingMessages.add(new DispatcherRequestMessage(
				new ConcreteNodeDescriptor("dest"),
				new MyApi(),
				new ConcreteRequester(null, null, null)));
		incomingMessages.add(new GeneralMessage());
		incomingMessages.add(new GeneralMessage());
		incomingMessages.add(new GeneralMessage());
		incomingMessages.add(new DispatcherRequestMessage(
				new ConcreteNodeDescriptor("dest"),
				new MyApi(),
				new ConcreteRequester(null, null, null)));
		incomingMessages.add(new DispatcherRequestMessage(null, null, null));
		incomingMessages.add(new GeneralMessage());
		
		registeredServices = new Vector<Service>();
	}


	/**
	 * Test method for {@link systems.network.GeneralSystemsNetManager#extractIncomingMessages(java.util.Queue)}.
	 */
	@Test
	public void testExtractServiceMessages() {
		Queue<Message> queueMessage;
		countMessages();
		sysNetMan.extractIncomingMessages(incomingMessages);
		
		queueMessage = sysNetMan.getReceivedQueueMessage();
		assertEquals(numberDispReq, queueMessage.size());
		
		RequestMessage msg = (RequestMessage) queueMessage.poll();
		assertNotNull(msg);
		msg.getFunction().initializeServices(registeredServices);
		
		msg = (RequestMessage) queueMessage.poll();
		assertNotNull(msg);
		msg.getFunction().initializeServices(registeredServices);
		
//		fail("Not yet implemented");
	}

	@Test
	public void testDispatcher(){
		DispatcherSystem disp = new DispatcherSystem(new FakeNetMan(), new Vector<Service>());
		disp.start();
	}
	
	/**
	 * 
	 */
	private void countMessages() {
		for(Message m : incomingMessages){
			Class<?> cl = m.getClass();
			if(cl.equals(GeneralRequestMessage.class))
				numberRequests++;
			if(cl.equals(DispatcherRequestMessage.class))
				numberDispReq++;
		}
		print("found: \n"+
				numberRequests+" GeneralRequestMessage, "+  numberDispReq+" dipatcherRequestMessages ");
	}
	

	private void print(Object o){
		System.out.println(o);
	}
	


}
